#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using i128 = __int128;
const int N = 1e6 + 5;

void solve() {
    int n, m, choic, a;
    deque<int> dp;
    cin >> n >> m;
    while (m--) {
        cin >> choic;
        switch (choic) {
            case 1:
                cin >> a;
                dp.push_front(a);
                break;
            case 2:
                dp.pop_front();
                break;
            case 3:
                cin >> a;
                dp.push_back(a);
                break;
            case 4:
                dp.pop_back();
                break;
            case 5:
                reverse(dp.begin(), dp.end());
                break;
            case 6:
                cout << dp.size() << endl;
                for (int i = 0; i < dp.size(); i++) {
                    if (i) cout << " ";
                    cout << dp[i];
                }
                cout << endl;
                break;
            case 7:
                sort(dp.begin(), dp.end());
                break;
        }
    }
    return;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    solve();

    return 0;
}